package com.musifind.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.musifind.adapters.FavouriteListAdapter;
import com.musifind.common.GeneralHelper;
import com.musifind.models.FavouriteItems;
import com.musifind.utils.FavouritesDatabase;

public class FavouritesActivity extends Activity {

	private TextView txtNoFavourites;
	private ListView lstFavourites;

	private String trackURL;
	private String trackId;
	private String coverImage;
	private String trackName;
	private String artistName;
	private String albumName;
	private ProgressDialog progressDialog = null;
	FavouritesDatabase dbFavs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favourites);

		// activity starts as push old activity to left
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

		dbFavs = new FavouritesDatabase(FavouritesActivity.this);

		lstFavourites = (ListView) findViewById(R.id.lstFavouriteTracks);
		txtNoFavourites = (TextView) findViewById(R.id.txtNoFavourites);

		showList();

		lstFavourites.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				trackId = ((FavouriteItems) parent.getAdapter().getItem(position)).getTrackId();
				trackURL = ((FavouriteItems) parent.getAdapter().getItem(position)).getTrackURL();
				coverImage = ((FavouriteItems) parent.getAdapter().getItem(position)).getCoverImage();
				trackName = ((FavouriteItems) parent.getAdapter().getItem(position)).getTrackName();
				artistName = ((FavouriteItems) parent.getAdapter().getItem(position)).getArtistName();
				albumName = ((FavouriteItems) parent.getAdapter().getItem(position)).getAlbumName();
				showAlertDialog(trackId, trackURL, coverImage, trackName, artistName, albumName);
			}
		});
	}

	private void showList() {

		ArrayList<FavouriteItems> favList = new ArrayList<FavouriteItems>();
		favList.clear();
		String query = "SELECT * FROM favs";
		Cursor c1 = dbFavs.selectQuery(query);
		if (c1 != null && c1.getCount() != 0) {
			if (c1.moveToFirst()) {
				do {
					txtNoFavourites.setVisibility(View.INVISIBLE);
					FavouriteItems favouriteItems = new FavouriteItems();

					favouriteItems.setTrackId(c1.getString(c1.getColumnIndex("trackId")));
					favouriteItems.setTrackURL(c1.getString(c1.getColumnIndex("trackURL")));
					favouriteItems.setCoverImage(c1.getString(c1.getColumnIndex("coverImage")));
					favouriteItems.setTrackName(c1.getString(c1.getColumnIndex("trackName")));
					favouriteItems.setArtistName(c1.getString(c1.getColumnIndex("artistName")));
					favouriteItems.setAlbumName(c1.getString(c1.getColumnIndex("albumName")));
					favList.add(favouriteItems);

				} while (c1.moveToNext());
			}
		}
		c1.close();

		FavouriteListAdapter favListAdapter = new FavouriteListAdapter(FavouritesActivity.this,
				R.layout.favourite_list_items_row_tracks, favList);
		lstFavourites.setAdapter(favListAdapter);

	}

	public void showAlertDialog(final String trackId, final String trackURL, final String coverImage,
			final String trackName, final String artistName, final String albumName) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FavouritesActivity.this);
		alertDialogBuilder.setTitle("Uyarı").setMessage("Ne yapmak istiyorsunuz?").setCancelable(true)
				.setPositiveButton("Dinle", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						new OpenTrackAsyncTask().execute();
						dialog.dismiss();
					}
				}).setNegativeButton("Kaldır", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						removeSong(trackId);
						dialog.dismiss();
						showList();
					}

				});

		final AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.show();
	}

	public void removeSong(String trackId) {
		dbFavs.deleteFavourite(trackId);
		GeneralHelper.showPurpleToast(getApplicationContext(), "Şarkı favorilerinizden kaldırıldı");
	}

	public void listenSong(String trackId, String trackURL, String coverImage, String trackName, String artistName,
			String albumName) {
		Intent i = new Intent(FavouritesActivity.this, MusicPlayerActivity.class);
		i.putExtra("track_id", trackId);
		i.putExtra("track_url", trackURL);
		i.putExtra("cover_image", coverImage);
		i.putExtra("track_name", trackName);
		i.putExtra("artist_name", artistName);
		i.putExtra("album_name", albumName);
		startActivity(i);
	}

	public class OpenTrackAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(FavouritesActivity.this);
			progressDialog.setTitle("Çalmaya hazırlanıyor");
			progressDialog.setMessage("Lütfen bekleyiniz...");
			progressDialog.setCancelable(false);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			listenSong(trackId, trackURL, coverImage, trackName, artistName, albumName);
			return null;
		}

		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();
		}
	}
}
